package org.shock.ss.sms.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.shock.ss.sms.bean.system.UserInfor;

public class LoginFilter implements Filter {
	private static Logger log = Logger.getLogger(LoginFilter.class);

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		try {
			HttpSession session = request.getSession();
			UserInfor obj = (UserInfor) session.getAttribute("userinfor");
			String reqUrl = request.getServletPath();
			log.debug(reqUrl);
			if ("/helpdocAdmin.do".equals(reqUrl) || "/userLogout.do".equals(reqUrl) || "/userLogin.do".equals(reqUrl)
					|| "/login.jsp".equals(reqUrl) || "/logout.jsp".equals(reqUrl)
					|| "/bbtj/customer_self.jsp".equals(reqUrl) || reqUrl.contains("/public/")) {
				chain.doFilter(request, response);
			} else {
				if (obj == null || null == obj.getYgzh()) {
					response.sendRedirect("logout.jsp");
				} else {
					chain.doFilter(request, response);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.sendRedirect("logout.jsp");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

	}

	public void init(FilterConfig arg0) throws ServletException {

	}

}
